﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AudioCore.Entity;
using AudioCore.DAO;
using System.Data;

namespace AudioCore.Biz
{
    public class CategoryArticleBiz : BaseBiz
    {
        public int AddCategoryArticle(CategoryArticleEntity entity)
        {
            try
            {
                using (CategoryArticleDao daoCategory = new CategoryArticleDao())
                {
                    return daoCategory.InsertEntity(entity, typeof(CategoryArticleEntity));
                }
            }
            catch (Exception ex)
            {
            }
            return 0;
        }

        public int UpdateSortOrder(int categoryID, int articleID, DateTime sortOrder)
        {
            try
            {
                using (CategoryArticleDao daoCategory = new CategoryArticleDao())
                {
                    return daoCategory.UpdateSortOrder(categoryID, articleID, sortOrder);
                }
            }
            catch (Exception ex)
            {
            }
            return 0;
        }

        public int DeleteCategoryArticle(int categoryArticleID)
        {
            try
            {
                using (CategoryArticleDao daoCategory = new CategoryArticleDao())
                {
                    return daoCategory.DeleteEntity(categoryArticleID);
                }
            }
            catch (Exception ex)
            {
            }
            return 0;
        }

        public CategoryArticleEntity GetCategoryArticleByID(int categoryArticleID)
        {
            try
            {
                using (CategoryArticleDao daoCategoryArticle = new CategoryArticleDao())
                {
                    DataTable table = daoCategoryArticle.SelectEntityByID(categoryArticleID);
                    if (table.Rows.Count > 0)
                    {
                        return (CategoryArticleEntity)BaseBiz.MapDataRowToObject(table.Rows[0], typeof(CategoryArticleEntity));
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        public List<CategoryArticleEntity> GetCategoryArticleByCategoryID(int categoryID)
        {
            try
            {
                using (CategoryArticleDao daoCategoryArticle = new CategoryArticleDao())
                {
                    DataTable table = daoCategoryArticle.SelectEntityByColumnName("CategoryID", categoryID);
                    return BaseBiz.MapDataTableToList(table, typeof(CategoryArticleEntity)).ConvertAll(item => (CategoryArticleEntity)item);
                }
            }
            catch (Exception ex)
            {
            }
            return new List<CategoryArticleEntity>();
        }

        public List<CategoryArticleEntity> GetCategoryArticleByArticleID(int articleID)
        {
            try
            {
                using (CategoryArticleDao daoCategoryArticle = new CategoryArticleDao())
                {
                    DataTable table = daoCategoryArticle.SelectEntityByColumnName("CategoryID", articleID);
                    return BaseBiz.MapDataTableToList(table, typeof(CategoryArticleEntity)).ConvertAll(item => (CategoryArticleEntity)item);
                }
            }
            catch (Exception ex)
            {
            }
            return new List<CategoryArticleEntity>();
        }

        /// <summary>
        /// Get list extendeds item (Item included CategoryName) by categoryID
        /// </summary>
        /// <param name="categoryID">CategoryID</param>
        /// <returns></returns>
        public List<CategoryArticleEntity> GetExtendedItemByArticleID(int articleID)
        {
            try
            {
                using (CategoryArticleDao daoCategoryArticle = new CategoryArticleDao())
                {
                    DataTable table = daoCategoryArticle.GetExtendedItemByArticleID(articleID);
                    return BaseBiz.MapDataTableToList(table, typeof(CategoryArticleEntity)).ConvertAll(item => (CategoryArticleEntity)item);
                }
            }
            catch (Exception ex)
            {
            }
            return new List<CategoryArticleEntity>();
        }

        public List<CategoryArticleEntity> GetMappingCategoryArticle(int articleID)
        {
            try
            {
                using (CategoryArticleDao dao = new CategoryArticleDao())
                {
                    DataTable table = dao.GetMappingCategoryArticle(articleID);
                    if (table.Rows.Count > 0)
                    {
                        return BaseBiz.MapDataTableToList(table, typeof(CategoryArticleEntity)).ConvertAll(item => (CategoryArticleEntity)item);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return new List<CategoryArticleEntity>();
        }

        public CategoryArticleEntity GetItemByCategoryANDArticle(int categoryID, int articleID)
        {
            try
            {
                using (CategoryArticleDao dao = new CategoryArticleDao())
                {
                    string[] columns = new string[] { "CategoryID", "ArticleID" };
                    object[] values = new object[] { categoryID, articleID };
                    DataTable table = dao.SelectEntityByColumnNames(columns, values);
                    if (table.Rows.Count > 0)
                    {
                        return (CategoryArticleEntity)BaseBiz.MapDataRowToObject(table.Rows[0], typeof(CategoryArticleEntity));
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return null;
        }
    }
}
